package LinkData1;

public class LinkedList {

    ListNode head;

    public LinkedList() {
        this.head = null;
    }

    /**
     * 插入节点  末尾
     */
    public void insertNode(int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
        } else {
            ListNode current = head;
            while (current.Next != null) {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }

    /**
     * 删除节点
     */
    public void delete(int val) {
        if (head == null) {
            return;
        }
        if (head.val == val) {
            head = head.Next;
            return;
        }
        ListNode current = head;
        while (current.Next != null) {
            if (current.Next.val == val) {
                current.Next = current.Next.Next;
                return;
            }
            current = current.Next;
        }

    }

    /**
     * 打印节点
     */
    public void printList() {
        ListNode current = head;
        while (current != null) {
            System.out.print(current.val + "->");
            current = current.Next;
        }
        System.out.print("null");
        System.out.println();
    }

}
